angular.module("app").service("userInfo", ["utils", "$http", "$q", "$state", "$rootScope", "product", function(utils, $http, $q, $state, $rootScope, product) {
	this.phone = null //手机号
	this.user_code_temp = null //临时保存user_code
	this.user_code = null //用户id
	this.user_type = null //用户类型
	this.acc_amount = null //钱包余额
	this.acc_id = null //账户id
	this.is_new_user = 0 //是否新手
	this.acc_number = null //银行卡号
	this.acc_number = null //银行卡号
	this.payamount = null //充值金额
	this.pay_platform_type = null //支付方式
	this.order_id = null //充值订单id
	this.order_type = null //订单类型
	this.buy_type = null
	this.cashInfo = {}
	this.backPwd = false //是否是充值交易密码
	this.risklevel = null

	/*获取用户身份信息*/
	this.getUserIdentity = function() {
		var defer = $q.defer()
		if(this.user_code) {
			$http.get(utils.userSys, {
				params: {
					header: utils.header("070070"),
					body: {
						birth_day: "",
						home_address: "",
						id_number: "",
						id_type: "",
						marry_status: "",
						operation_type: 1,
						phone_id: "",
						phone_number: "",
						real_name: "",
						sex: "",
						user_code: this.user_code
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					defer.resolve(res.body)
				}
			})
		}

		return defer.promise
	}

	/*获取用户状态*/
	this.getUserInfo = function() {
		var defer = $q.defer()
		if(this.user_code) {
			$http.get(utils.userSys, {
				params: {
					header: utils.header("070117"),
					body: {
						check_type: "0",
						phone_number: "",
						user_code: this.user_code,
						phone_type: ""
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					defer.resolve(res.body)
				}
			})
		}

		return defer.promise
	}
	/*获取用户绑卡信息*/
	this.getUserBank = function() {
		var _this = this
		var defer = $q.defer()
		var promise = this.getUserInfo()
		promise.then(function(res) {
			if(res.is_bind_card != 0 && _this.user_code || res.card_bind_status == 3) {
				$http.get(utils.userSys, {
					params: {
						header: utils.header("070113"),
						body: {
							acc_type_id: 1,
							user_code: _this.user_code
						}
					}
				}).then(function(res) {
					if(res.header.re_code == "0000") {
						defer.resolve(res)
					}
				})
			}
		})

		return defer.promise
	}

	/*获取是否新手*/
	this.getUserIsNew = function() {
		/*获取用户资产以及状态信息*/
		var _this = this
		if(this.user_code) {
			var promise = this.getUserInfo()
			promise.then(function(res) {
				_this.is_new_user = res.is_new_user
			})
		}
	}

	/*购买*/
	this.buy = function(item) {
		var _this = this
		/*未登录*/

		if(!this.user_code) {
			$state.go("register")
			return
		}

		if(this.is_new_user != 0 && item.is_new_user_mark == 0) {
			return utils.msg("老用户不能购买新手标！")
		}
		/*判断是否实名绑定*/
		var promise = this.getUserInfo()
		promise.then(function(res) {

			/*特殊状态*/
			if(res.card_bind_status == 3) {
				$state.go("ValidatePhone")
				return
			}

			/*未实名*/
			if(res.is_real_name == 0) {
				$state.go("Realname")
				return
			}
			/*未绑卡*/
			if(res.is_bind_card == 0) {
				$state.go("BindBank")
				return
			}
			/*未设置交易密码*/
			if(res.is_tra_password == 0) {
				$state.go("SetTradePassword")
				return
			}

			/*未进行风险评测*/
			$http.get(utils.productSys, {
				params: {
					header: utils.header("030508"),
					body: {
						risk_level: item.risk_level,
						user_code: _this.user_code
					}
				}
			}).then(function(res) {
				if(res.header.re_code == "0000") {
					_this.risklevel = res.body.user_level
					var info = ""
					switch(res.body.user_level) {
						case 1:
							info = "保守型"
							break;
						case 2:
							info = "稳健型"
							break;
						case 3:
							info = "平稳型"
							break;
						case 4:
							info = "成长型"
							break;
						case 5:
							info = "进取型"
							break;
					}

					if(res.body.user_validat == 1) { //未进行风险评测
						mui.confirm('您还未进行风险等级评测，请评测完成后再进行购买', ' ', ['以后再说', '去测评'], function(e) {
							if(e.index === 1) {
								$state.go("02_Evaluation")
								return
							}
						}, 'div')
						return
					}

					if(res.body.risk_validat == 1) { //风险等级不够
						mui.confirm('<span style="text-align:left">该产品需风险测评结果为<b>' + item.user_level + '</b>才可购买，您的风险测评结果为<b>“' + info + '”</b>，不能购买</span>', ' ', ['我知道了', '查看测评结果'], function(e) {
							if(e.index === 1) {
								$state.go("02_Evaluation")
								return
							}
						}, 'div')
						return
					}

					$rootScope.isShow = true

				}
			})

		})
		this.payamount = null //清空金额
		product.Coupon = false //清空红包优惠券

	}

	/*红包排序*/
	this.sort = function(amount, product, arr) {
		var newArr = []

		for(var i = 0; i < arr.length; i++) {
			if(arr[i].coupon_type == 1) {
				//如果购买金额小于红包额度
				if(amount < arr[i].coupon_value || amount < arr[i].least_amount) {
					arr[i].available = 0
					continue
				}
				//红包期限不匹配
				if(arr[i].upper_term != 0 || arr[i].floor_term != 0) {
					if(product.term < arr[i].floor_term) {
						arr[i].available = 0
						continue
					}
					if(product.term > arr[i].upper_term && arr[i].upper_term != 0) {
						arr[i].available = 0
						continue
					}

				} else {
					//如果最大期限等于0,最小期限不等于0
					if(arr[i].floor_term != 0) {
						if(product.term < arr[i].floor_term) {
							arr[i].available = 0
							continue
						}

					}
				}
				arr[i].available = 1
			} else {

				/*单笔投资金额小于加息劵限制金额*/
				if(amount < arr[i].least_amount) {
					arr[i].available = 0
					continue
				}
				/*加息时长大于产品时长*/
				if(arr[i].increase_term > product.term) {
					arr[i].available = 0
					continue
				}
				arr[i].available = 1

			}

		}
		newArr = arr
		return newArr

	}

}])